package com.yand.frame.service.impl;


import com.yand.frame.dao.ActivityMenuMapper;
import com.yand.frame.entity.ActivityMenu;
import com.yand.frame.service.MenuService;
import com.yand.frame.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private ActivityMenuMapper activityMenuMapper;

    @Autowired
    private TreeUtil treeUtil;


    /**
     * 获取所有的菜单的方法
     * @return
     */
    @Override
    public List<ActivityMenu> getAllMenuList() {
        List<ActivityMenu> menuList = activityMenuMapper.queryMenuList(0);
        menuList = (List<ActivityMenu>)treeUtil.getRecursionList(menuList);
        return menuList;
    }

    @Override
    public ActivityMenu getActivityMenuById(int menuId) {
        ActivityMenu menu = null;
        if(menuId == 0){
            menu = new ActivityMenu();
            menu.setId(0);
            menu.setMenuName("根节点");
            return menu;
        }
        //获取当前的菜单名称
        menu =  activityMenuMapper.selectByPrimaryKey(menuId);
        Integer parentMenuId = menu.getParentId();
        //判断是否是根节点
        if(parentMenuId != 0){
            ActivityMenu parentMenu = activityMenuMapper.selectByPrimaryKey(parentMenuId);
            menu.setParentMenu(parentMenu.getMenuName());
        }else{
            menu.setParentMenu("根节点");
        }

        return menu;
    }

    @Override
    public void updateMenu(ActivityMenu menu) {
        activityMenuMapper.updateByPrimaryKeySelective(menu);
    }


    @Override
    public void addMenu(ActivityMenu menu) {
        activityMenuMapper.insertMenu(menu);
    }
}
